package com.ctgu.tengyun.bean.convert;

import com.ctgu.tengyun.bean.response.SaleOrderProductInfo;
import com.ctgu.tengyun.bean.response.SaleProductListResponse;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.domain.SaleOrderProductRecord;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by liaoweizhi on 2019/8/8.
 */
public class SaleOrderConvert {

    public static SaleProductListResponse toSaleProductListResponse(Integer totalCount,
                                                                    Integer pageSize,
                                                                    List<SaleOrderProductRecord> recordList,
                                                                    Map<String, ProductInfo> productNoMap) {
        SaleProductListResponse response = new SaleProductListResponse();

        response.setTotalCount(totalCount);
        response.setTotalPage((int) Math.ceil(totalCount / (double) pageSize));

        List<SaleOrderProductInfo> saleOrderProductInfoList = new ArrayList<>();
        for (SaleOrderProductRecord record : recordList) {
            saleOrderProductInfoList.add(toSaleOrderProductInfo(record, productNoMap.get(record.getProductNo())));
        }
        response.setRecordList(saleOrderProductInfoList);

        return response;
    }

    public static SaleOrderProductInfo toSaleOrderProductInfo(SaleOrderProductRecord record,
                                                              ProductInfo productInfo) {
        SaleOrderProductInfo saleInfo = new SaleOrderProductInfo();
        saleInfo.setSaleRecord(record);
        saleInfo.setProductInfo(productInfo);

        return saleInfo;
    }

}
